<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
                :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="订单号" prop="transactionId">
            <el-input v-model="queryParams.topUpId" placeholder="订单号" clearable style="width: 240px"
                      @keyup.enter="handleQuery"/>
          </el-form-item>
          <el-form-item label="流水号" prop="transactionId">
            <el-input v-model="queryParams.transactionId" placeholder="第三方支付系统生成的流水号" clearable
                      style="width: 240px" @keyup.enter="handleQuery"/>
          </el-form-item>
          <el-form-item label="支付时间" prop="successTime">
            <el-date-picker clearable
                            v-model="queryParams.successTime"
                            type="date"
                            value-format="YYYY-MM-DD"
                            placeholder="请选择支付完成时间"
            />
          </el-form-item>
          <el-form-item label="支付渠道" prop="payWay">
            <el-select v-model="queryParams.payWay" placeholder="请输入支付渠道" clearable>
              <el-option
                v-for="dict in top_up_way"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="订单状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable>
              <el-option
                v-for="dict in top_up_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="订单类型" prop="orderType">
            <el-select v-model="queryParams.orderType" placeholder="请选择业务订单类型" clearable>
              <el-option
                v-for="dict in top_up_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()"
                       v-hasPermi="['pay:topUp:remove']">删除
            </el-button>
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['pay:topUp:export']">
              导出
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
<!--      <template #header>-->
<!--        <el-row :gutter="10" class="mb8">-->
<!--          <el-col :span="1.5">-->

<!--          </el-col>-->
<!--          <el-col :span="1.5">-->

<!--          </el-col>-->
<!--          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>-->
<!--        </el-row>-->
<!--      </template>-->

      <el-table v-loading="loading" :data="topUpList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="充值订单号" align="center" prop="topUpId" width="180"/>
        <el-table-column label="第三方支付流水号" align="center" prop="transactionId" width="180"/>
        <el-table-column label="订单总金额(元)" align="center" prop="total"/>
        <el-table-column label="用户支付金额(元)" align="center" prop="payerTotal"/>
        <!--        <el-table-column label="用户支付币种" align="center" prop="payerCurrency"/>-->
        <el-table-column label="支付渠道" align="center" prop="payWay">
          <template #default="scope">
            <dict-tag :options="top_up_way" :value="scope.row.payWay"/>
          </template>
        </el-table-column>
        <el-table-column label="订单状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="top_up_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="交易状态" align="center" prop="tradeState"/>
        <el-table-column label="订单类型" align="center" prop="orderType">
          <template #default="scope">
            <dict-tag :options="top_up_type" :value="scope.row.orderType"/>
          </template>
        </el-table-column>
        <el-table-column label="支付时间" align="center" prop="successTime" width="180"/>
        <el-table-column label="退款时间" align="center" prop="updateTime" width="180">
          <template #default="scope">
            <span v-show="scope.row.status == 3">{{ scope.row.updateTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
          <template #default="scope">
            <el-tooltip content="退款" placement="top">
              <el-button link type="primary" icon="Position" @click="handleRefund(scope.row.topUpId)"
                         v-hasPermi="['pay:topUp:edit']">退款
              </el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)"
                         v-hasPermi="['pay:topUp:remove']">删除
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

  </div>
</template>

<script setup name="TopUp" lang="ts">
import {listTopUp, getTopUp, delTopUp, addTopUp, updateTopUp, refund} from '@/api/pay/topUp';
import {TopUpVO, TopUpQuery, TopUpForm} from '@/api/pay/topUp/types';

const {proxy} = getCurrentInstance() as ComponentInternalInstance;
const {
  top_up_status,
  top_up_type,
  top_up_way
} = toRefs<any>(proxy?.useDict('top_up_status', 'top_up_type', 'top_up_way'));

const topUpList = ref<TopUpVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: TopUpForm = {
  topUpId: undefined,
  transactionId: undefined,
  successTime: undefined,
  total: undefined,
  payerTotal: undefined,
  payerCurrency: undefined,
  payWay: undefined,
  status: undefined,
  tradeState: undefined,
  orderJson: undefined,
  orderType: undefined,
}
const data = reactive<PageData<TopUpForm, TopUpQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    topUpId: undefined,
    transactionId: undefined,
    successTime: undefined,
    total: undefined,
    payerTotal: undefined,
    payerCurrency: undefined,
    payWay: undefined,
    status: undefined,
    tradeState: undefined,
    orderJson: undefined,
    orderType: undefined,
    params: {}
  },
});

const {queryParams, form, rules} = toRefs(data);

/** 查询系统充值订单列表 */
const getList = async () => {
  loading.value = true;
  const res = await listTopUp(queryParams.value);
  topUpList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: TopUpVO[]) => {
  ids.value = selection.map(item => item.topUpId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 退款按钮操作 */
const handleRefund = async (topUpId: string) => {
  await proxy?.$modal.confirm('是否退款编号:' + topUpId + ' ？').finally(() => loading.value = false);
  await refund(topUpId);
  proxy?.$modal.msgSuccess("退款成功");
  await getList();
}
/** 删除按钮操作 */
const handleDelete = async (row?: TopUpVO) => {
  const _topUpIds = row?.topUpId || ids.value;
  await proxy?.$modal.confirm('是否确认删除系统充值订单编号为"' + _topUpIds + '"的数据项？').finally(() => loading.value = false);
  await delTopUp(_topUpIds);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('pay/topUp/export', {
    ...queryParams.value
  }, `topUp_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
